#!/user/bin/env python3
# -*- coding: utf-8 -*-
import datetime
import logging
import inspect
import os
import re
from logging.handlers import TimedRotatingFileHandler
from logging.handlers import RotatingFileHandler

'''
logger 框架记录日志
'''
class Logger():
    def __init__(self, ):
        base_path = os.getcwd()
        self.log_dir = base_path+"/logs"
        self.logger = None
        self.setup_logger()

    def setup_logger(self):
        # 创建日志目录
        if not os.path.exists(self.log_dir):
            os.makedirs(self.log_dir)
        # 获取当前日期
        today = datetime.date.today()
        # 拼接日志文件路径
        log_file_path = os.path.join(self.log_dir, f"mylog.log")
        # 创建日志记录器
        self.logger = logging.getLogger(__name__)
        self.logger.setLevel(logging.DEBUG)
        # 创建文件处理器
        self.handler = TimedRotatingFileHandler(log_file_path,when="midnight",backupCount=5, encoding="utf8")
        self.handler.suffix = "%Y%m%d_%H%M%S"
        # 创建格式化器
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
        self.handler.setFormatter(formatter)
        # 添加处理器到记录器
        if not self.logger.handlers:
            self.logger.addHandler(self.handler)
    def loginfo(self, message):
         method_name = inspect.currentframe().f_back.f_code.co_name
         line_no = inspect.currentframe().f_back.f_lineno
         self.logger.info(f"{method_name} ({line_no}): {message}")